package com.tanhua.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        User user = UserHolder.get();

        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setId(user.getId());
        Question question = questionApi.findByUserId(user.getId());
        settingsVo.setStrangerQuestion(question == null ? "你的小名叫什么？" : question.getTxt());
        settingsVo.setPhone(user.getMobile());
        Settings settings = settingsApi.findByUserId(user.getId());
        if (settings != null) {
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }

        return settingsVo;
    }

    public void saveQuestions(String content) {
        Long id = UserHolder.getId();
        Question question = questionApi.findByUserId(id);
        if (question == null) {
            question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    public PageResult queryBlackList(Integer page, Integer pagesize) {
        Long id = UserHolder.getId();
        Page<UserInfo> pageInfo = blackListApi.queryBlackList(page,pagesize,id);
        PageResult pageResult = new PageResult(pageInfo.getCurrent(),pageInfo.getSize(),pageInfo.getTotal(),pageInfo.getRecords());
        return pageResult;

    }

    public void removeBlackUser(Long blackUserId) {
        Long uid = UserHolder.getId();
        blackListApi.removeById(uid,blackUserId);
    }
}
